package com.metadata_system.api_server.system_server;

import com.metadata_system.api.annotations.CheckTokenUser;
import com.metadata_system.common.dto.ApiResult;
import com.metadata_system.common.http.request_dto.LoginUserRequest;
import com.metadata_system.system.request.MessageListParams;
import com.metadata_system.system.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/system-server/v001/Message/")
public class MessageController {
    @Autowired
    LoginUserRequest loginUserRequest;
    @Autowired
    MessageService messageService;

    @RequestMapping(value = "getList", method = {RequestMethod.POST, RequestMethod.GET})
    @CheckTokenUser
    public ApiResult getList(
            @RequestParam(required = false) Integer projectId,
            @RequestParam(required = false) String projectName,
            @RequestParam(required = false) Integer isRead,
            @RequestParam(required = false) String type,
            @RequestParam(required = false) String content,
            @RequestParam(required = false) String createdTime,
            @RequestParam(required = false) Integer page,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) String sortProp,
            @RequestParam(required = false) String sortOrder
    ) {
        return messageService.getList(MessageListParams.builder()
                .userId(loginUserRequest.getTokenUser().decodeID())
                .projectId(projectId)
                .projectName(projectName)
                .isRead(isRead)
                .type(type)
                .content(content)
                .createdTime(createdTime)
                .page(page)
                .pageSize(pageSize)
                .sortProp(sortProp)
                .sortOrder(sortOrder)
                .build());
    }

    @RequestMapping(value = "read", method = {RequestMethod.POST, RequestMethod.GET})
    @CheckTokenUser
    public ApiResult read(
            @RequestParam(required = false) Integer id
    ) {
        return messageService.read(id, loginUserRequest.getTokenUser().decodeID());
    }


    @RequestMapping(value = "readAll", method = {RequestMethod.POST, RequestMethod.GET})
    @CheckTokenUser
    public ApiResult readAll() {
        return messageService.readAll(loginUserRequest.getTokenUser().decodeID());
    }
}
